package database

import (
	"fmt"
	"gormdemo/config"

	// "github.com/jinzhu/gorm"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func NewDatabase() *gorm.DB {

	HOST := config.Config.GetString("mysql.host")
	PORT := config.Config.GetString("mysql.port")
	USER := config.Config.GetString("mysql.user")
	PASS := config.Config.GetString("mysql.password")
	DBNAME := config.Config.GetString("mysql.db")

	URL := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", USER, PASS,
		HOST, PORT, DBNAME)
	db, err := gorm.Open(mysql.Open(URL))

	if err != nil {
		panic("Failed to connect to database!")

	}
	fmt.Println("Database connection established")
	return db
}
